package com.xiaoshuidi.cloud.module.finance.mq.consumer;

import com.google.common.primitives.Longs;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.tenant.core.util.TenantUtils;
import com.xiaoshuidi.cloud.module.finance.enums.finance.SourceBizTypeEnum;
import com.xiaoshuidi.cloud.module.finance.mq.PayResultConstants;
import com.xiaoshuidi.cloud.module.finance.mq.message.PayResultMessage;
import com.xiaoshuidi.cloud.module.finance.service.finance.FinanceAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.function.Consumer;

@Slf4j
@Component(PayResultConstants.CONSUMER)
public class PayResultConsumer implements Consumer<PayResultMessage> {
    @Resource
    private FinanceAccountService financeAccountService;

    @Override
    public void accept(PayResultMessage payResultMessage) {
        log.info("支付成功回调 {}", JsonUtils.toJsonString(payResultMessage));
        if (SourceBizTypeEnum.ACCOUNT_RECHARGE.name().equals(payResultMessage.getSourceBizType())) {
            Long journalId = Longs.tryParse(payResultMessage.getSourceBizId());
            if (journalId == null) {
                log.error("{} {} sourceBizId不正确", payResultMessage.getFlowNo(), payResultMessage.getSourceBizId());
                return;
            }
            TenantUtils.execute(payResultMessage.getTenantId(), () -> financeAccountService.journalPayCallback(journalId, payResultMessage));
        } else {

        }
    }
}
